/*
 * Copyright (c) 2007 Universidade Federal de Campina Grande
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.edu.ufcg.threadcontrol.aspects;

import java.util.List;

import br.edu.ufcg.threadcontrol.ThreadConfiguration;
import br.edu.ufcg.threadcontrol.ThreadControl;
import br.edu.ufcg.threadcontrol.ThreadWatcher;

public aspect ThreadControlAspect {

	/*
	 * The object responsible for storing lists of threads according to their
	 * states.
	 */
	private ThreadWatcher threadWatcher = new ThreadWatcher();

	void around(ThreadControl tcs): execution(public void ThreadControl.waitUntilStateIsReached()) && this (tcs){
		threadWatcher.waitUntilThreadConfiguration(tcs
				.getThreadsConfiguration());
	}

	void around(ThreadControl tcs): execution(public void ThreadControl.proceed()) && this (tcs){
		threadWatcher.proceed();
	}

	void around(ThreadControl tcs): execution(public void ThreadControl.reset()) && this (tcs){
		threadWatcher.reset();
	}

	after(List<ThreadConfiguration> threadsConfiguration): execution(public void ThreadControl.prepare(List<ThreadConfiguration>)) && args(threadsConfiguration){
		threadWatcher.prepare(threadsConfiguration);
	}

	pointcut excludedEntities():
        !within(br.edu.ufcg.threadcontrol.ThreadWatcher);

	pointcut threadStartCalls(Thread t):call(public void start())&& target(t) && excludedEntities();

	pointcut waitCalls(Object o):call(public void wait())&& target(o) && excludedEntities();

	pointcut runnableRunExecutions():execution(public void Runnable+.run()) && excludedEntities();

	pointcut sleepCalls(): call (public static void Thread.sleep(long)) && excludedEntities() && !within(junit.framework.TestCase+);

	pointcut timedWaitCalls(Object o):call(public void wait(long))&& target(o) && excludedEntities();

	/*
	 * ADVICE
	 */

	before(): sleepCalls(){
		threadWatcher.includeInSleepingThreads(Thread.currentThread());
	}

	after():sleepCalls(){
		threadWatcher.verifyThread();
		threadWatcher.removeFromSleepingThreads(Thread.currentThread());
		threadWatcher.verifyThread();
	}

	before(Thread t): threadStartCalls(t) {
		threadWatcher.includeInStartedThreads(t);
	}

	before(): runnableRunExecutions() {
		String sourceClass = thisJoinPoint.getSourceLocation().getWithinType()
				.getCanonicalName();
		Thread t = Thread.currentThread();
		threadWatcher.addRunnableClassName(t, sourceClass);
		threadWatcher.verifyThread();
		threadWatcher.includeInRunningThreads(t);
		threadWatcher.verifyThread();
	}

	after(): runnableRunExecutions() {
		String sourceClass = thisJoinPoint.getSourceLocation().getWithinType()
				.getCanonicalName();
		if (sourceClass.equals(threadWatcher.getRunnableClassName(Thread
				.currentThread()))) {
			threadWatcher.verifyThread();// ver se � pra tirar
			threadWatcher.removeRunnableThread();
			threadWatcher.verifyThread();
		}
	}

	before(Object o):waitCalls(o) && excludedEntities() {
		threadWatcher.addWaiting(o);
	}

	before(Object o):timedWaitCalls(o) && excludedEntities() {
		threadWatcher.addWaiting(o);
	}

	after(Object o):timedWaitCalls(o) && excludedEntities() {
		threadWatcher.verifyThread();
		Thread t = Thread.currentThread();
		threadWatcher.threadFinishedToWaitOnObject(o, true);
		threadWatcher.verifyThread();
	}

	after(Object o): waitCalls(o) && excludedEntities() {
		// TODO: Verify.
		threadWatcher.verifyThread();
		Thread t = Thread.currentThread();
		threadWatcher.threadFinishedToWaitOnObject(o, false);
		threadWatcher.verifyThread();
	}

	before(Object o):(call(public void notifyAll()))&& target(o) && excludedEntities() {
		this.threadWatcher.verifyThread();
		threadWatcher.notifyAllWaitingThreads(o);
	}

	before(Object o):(call(public void notify()))&& target(o) && excludedEntities() {
		this.threadWatcher.verifyThread();
		threadWatcher.notifyOneWaitingThread(o);
	}
}
